package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.*;
import com.study.selection.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
    // 注入Service
    @Autowired
    private MajorService majorService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("/listAll")
    public String doList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<Student> list = studentService.findAll();
        PageInfo<Student> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/admin/student.jsp";
    }
    // 根据关键字查询,绑定数据。
    @RequestMapping("/search")
    public String doSearch(@RequestParam(value = "page",defaultValue = "1")Integer page,String key,Model model){
        PageHelper.startPage(page,5);
        List<Student> list = studentService.findByKey(key);
        PageInfo<Student> info = new PageInfo<>(list,3);
        model.addAttribute("info",info);
        model.addAttribute("key",key);
        return "/view/admin/student.jsp";
    }
   // 添加对象,接收表单参数,执行添加，重定向到listAll
    @RequestMapping("/add")
    public String doAdd(Student student){
        boolean result = studentService.addStudent(student);
        return "redirect:listAll";
    }
    @RequestMapping("/getAdd")
    public String doAdd(Student student,Model model){
        model.addAttribute("student",student);
        List<College> collegeList = collegeService.findAll();
        model.addAttribute("collegeList",collegeList);
        List<Major> majorList = majorService.findAll();
        model.addAttribute("majorList",majorList);
        List<Classes> classesList = classesService.findAll();
        model.addAttribute("classesList",classesList);
        return "/view/admin/add_student.jsp";
    }
    // 根据主键获取单个对象。
    @RequestMapping("/getOne")
    public String doGetOne(int studentId,Model model){
        Student student= studentService.getByStudentId(studentId);
        model.addAttribute("student",student);
        List<College> collegeList = collegeService.findAll();
        model.addAttribute("collegeList",collegeList);
        List<Major> majorList = majorService.findAll();
        model.addAttribute("majorList",majorList);
        List<Classes> classesList = classesService.findAll();
        model.addAttribute("classesList",classesList);
        return "/view/admin/edit_student.jsp";
    }

//    // 修改对象，接收表单参数，执行修改，重定向到listAll
    @RequestMapping("/update")
    public String doUpdate(Student student){
        boolean result=studentService.updateStudent(student);
            return "redirect:listAll";
    }
//  // 删除对象，接收请求参数id,执行删除，重定向到listAll
    @RequestMapping("/delete")
    public String doDelete(int studentId){
        boolean result=studentService.deleteStudent(studentId);
        return "redirect:listAll";
    }
    @RequestMapping("/deleteItems")
    public String deleteItems(String[] chkIds) {
        for (String string : chkIds) {
            studentService.deleteStudent(Integer.parseInt(string));
        }
        return "redirect:listAll";
    }


}



